<?php
namespace app\hsycms\controller;
use think\Controller;
use app\hsycms\controller\Base;
use think\Db;

class Chtml extends Base
{
	public $mbispc;  //0电脑端 1手机端			
	public function __construct(){
		parent::__construct();
		$this->mbispc = input('htmlpath');		
	}	
	
	//生成首页
	public function index(){		
		if($_POST){
			action('index/index/doindex');
			$indexName = config('webindexname') ? config('webindexname') : 'index.html';
			$htmlpath = $this->mbispc==0?$indexName:config('webmhtmlpath').'/'.$indexName; 
			$this->filePutContents('./'.$htmlpath,'index');	
			return json(array('status'=>'y','info'=>'生成成功'));			
		}else{
			return $this->fetch();
		}
	}

	//生成栏目页
	public function lists(){
		$list = db('nav')->where('mid<>6 and isshow=1')->field('id,title,entitle')->order('sort,id')->select();
		foreach($list as $key=>$v){
			$list[$key]['count'] = db('article')->where('nid',$v['id'])->count();
		}
		$this->assign("list",$list);
		return $this->fetch("list");
	}			
	
	//生成栏目页处页
	public function doNav(){
		if($this->request->isPost()){
			$page = input('process_page','','intval');
			$nid  = input('process_nid','','intval');		
			$total= input('process_total','','intval');	
			$pagenid  = input('process_pagenid','','intval');
			if($total==0){
				$nid && $where['id']  = $nid;
				$list = db('nav')->field('id,entitle,page,showcate')->where($where)->limit($page-1,1)->order('sort,id')->select();
				if($list){
					$count = db('nav')->where($where)->count();
					$process = ceil($page/$count*100);
					$redata = array();
					$retotal = 0;
					$pagenid = '';
					foreach($list as $key=>$v){
						$tpl = $this->getlisttpl($v['entitle'],1);					
						if(file_exists($tpl['tpl'])){
							//栏目本身生成
							$re1 = $this->docreate($v['entitle'],$tpl['retpl'],1);
							array_push($redata,$re1);							
							//栏目生成第一页
							if($v['showcate']==1){
								$pagesize  = $this->getpagesize($v['id'],0);
								$totalpage = $this->checknidpage($v['id'],$pagesize);	
								if($totalpage>0){	
									action('index/article/doarticle',array('type'=>$v['entitle'],'p'=>1,'mbispc'=>$this->mbispc));
									$re2 	   = $this->docreate($v['entitle'],$tpl['retpl'],3,1);
									$retotal = $totalpage;
									$pagenid = $v['id'];
									array_push($redata,$re2);
								}
							}
						}else{
							$re3 = "模板文件不存在";
							array_push($redata,$re3);
						}
					}					
					return json(array('status'=>'1','info'=>'生成中','data'=>$redata,'process'=>$process,'totalpage'=>$retotal,'pagenid'=>$pagenid,'startpage'=>1));				
				}else{
					return json(array('status'=>'0','info'=>'加载完成','data'=>'生成完成'));
				}	
			}else{
				 //继续生成分类其他分页
				 $listcount = $page > $total ? 0 : $total; //分类分页总数
				 $process = 0;		
				 $redata = array();			
				 if($listcount>0){
					 $entitle = db('nav')->where(['id'=>$pagenid])->value('entitle');					
					 $process = ceil($page/$listcount*100);					
					 $retotal = $total;
					 $tpl = $this->getlisttpl($entitle,$page);
					 if(file_exists($tpl['tpl'])){
							action('index/article/doarticle',array('type'=>$entitle,'p'=>$page,'mbispc'=>$this->mbispc));
							$re2 = $this->docreate($entitle,$tpl['retpl'],3,$page);
							array_push($redata,$re2);
					 }else{
						$re3 = "模板文件不存在";
						array_push($redata,$re3);
					 }
					 return json(array('status'=>'1','info'=>'生成中','data'=>$redata,'process'=>$process,'totalpage'=>$listcount,'pagenid'=>$pagenid));
				 }else{
					 return json(array('status'=>'1','info'=>'分页生成完成','process'=>0,'totalpage'=>0));
				 }				 
			}			
		}else{
			$this->assign('nid',intval(input('process_nid')));		
			$this->assign('htmlpath',$this->mbispc);
			return $this->fetch('navcreate');
		}	
	}	
	
	
	//ajax获取分类信息
	public function getcate(){
		$nid = input('nid','','intval');
		$cate = treelist('cate','',['nid'=>$nid]);		
		return json(array('status'=>'0','info'=>'获取分类成功','data'=>$cate));
	}
	
	//生成分类页
	public function cate(){
		$list = db('nav')->where('mid<>6 and isshow=1 and showcate=1')->field('id,title,entitle')->order('sort,id')->select();
		foreach($list as $key=>$v){
			$list[$key]['count'] = db('cate')->where('nid',$v['id'])->count();
		}
		$this->assign("list",$list);
		return $this->fetch("cate");
	}	
	
	//判断栏目是否有分页
	public function checknidpage($nid,$pagesize){
		$w['nid'] = $nid;
		$total  = db('article')->where($w)->count();
		$totalpage = 0;
		if($total > 0){
			$totalpage = ceil($total/$pagesize); //获取总页数
		}
		return $totalpage;
	}
	
	//获取分页数 
	public function getpagesize($nid,$page){	
		$pagesize = 12;
		$npage = db('nav')->where(['id'=>$nid])->value('page');					
		if($npage>0){$pagesize = $npage;}						
		if($page>0){ $pagesize = $page; }	
		return $pagesize;
	}
	
	//判断分类是否有分页
	public function checkcidpage($cid,$pagesize){
		$w['cid'] = array('in',treecatewhere($cid));
		$total  = db('article')->where($w)->count();
		$totalpage = 0;
		if($total > 0){
			$totalpage = ceil($total/$pagesize); //获取总页数
		}
		return $totalpage;
	}
	
	//获取列表模板
	public function getlisttpl($entitle,$page){		
		$result = action('index/article/doarticle',array('type'=>$entitle,'p'=>$page,'mbispc'=>$this->mbispc));
		$retpl = $result['showcate']==1?'list'.$result['tpl']:'show'.$result['tpl']; //生成目录名称
		$htmlpath = $this->mbispc==0?config('webthemes'):config('webmthemes'); //html生成目录					
		$tpl = ROOT_PATH.'app/index/view/'.$htmlpath.'/'.$retpl.'.html';   //模板路径	
		$r['retpl'] = $retpl;
		$r['tpl']   = $tpl;
		return $r;
	}
	
	//生成分类页处页
	public function doCate(){
		if($this->request->isPost()){
			$page = input('process_page','','intval');
			$nid  = input('process_nid','','intval');		
			$total= input('process_total','','intval');
			$cid  = input('process_cid','','intval');
			$pagecid  = input('process_pagecid','','intval');					
			if($total==0){
				$nid && $where['nid']  = $nid;
				$cid && $where['id']   = $cid;
				$list = db('cate')->field('id,entitle,page,nid')->where($where)->limit($page-1,1)->order('sort,id')->select();
				if($list){
					$count = db('cate')->where($where)->count();
					$process = ceil($page/$count*100);
					$redata = array();
					$retotal = 0;
					$recid = '';
					foreach($list as $key=>$v){
						$tpl = $this->getlisttpl($v['entitle'],1);
						if(file_exists($tpl['tpl'])){
							//分页本身生成
							$re1 = $this->docreate($v['entitle'],$tpl['retpl'],1);
							array_push($redata,$re1);							
							//分类分页生成第一页
							$pagesize  = $this->getpagesize($v['nid'],$v['page']);
							$totalpage = $this->checkcidpage($v['id'],$pagesize);
							if($totalpage>0){	
								action('index/article/doarticle',array('type'=>$v['entitle'],'p'=>1,'mbispc'=>$this->mbispc));
								$re2 	   = $this->docreate($v['entitle'],$tpl['retpl'],3,1);
								$retotal = $totalpage;
								$recid   = $v['id'];
								array_push($redata,$re2);
							}
						}else{
							$re3 = "模板文件不存在";
							array_push($redata,$re3);
						}
					}					
					return json(array('status'=>'1','info'=>'生成中','data'=>$redata,'process'=>$process,'totalpage'=>$retotal,'pagecid'=>$recid,'startpage'=>1));				
				}else{
					return json(array('status'=>'0','info'=>'加载完成','data'=>'生成完成'));
				}	
			}else{
				 //继续生成分类其他分页
				 $listcount = $page > $total ? 0 : $total; //分类分页总数
				 $process = 0;		
				 $redata = array();			
				 if($listcount>0){
					 $entitle = db('cate')->where(['id'=>$pagecid])->value('entitle');					
					 $process = ceil($page/$listcount*100);					
					 $retotal = $total;
					 $tpl = $this->getlisttpl($entitle,$page);
					 if(file_exists($tpl['tpl'])){
							action('index/article/doarticle',array('type'=>$entitle,'p'=>$page,'mbispc'=>$this->mbispc));
							$re2 = $this->docreate($entitle,$tpl['retpl'],3,$page);
							array_push($redata,$re2);
					 }else{
						$re3 = "模板文件不存在";
						array_push($redata,$re3);
					 }
					 return json(array('status'=>'1','info'=>'生成中','data'=>$redata,'process'=>$process,'totalpage'=>$listcount,'pagecid'=>$pagecid));
				 }else{
					 return json(array('status'=>'1','info'=>'分页生成完成','process'=>0,'totalpage'=>0));
				 }				 
			}			
		}else{
			$this->assign('nid',intval(input('process_nid')));
			$this->assign('cid',intval(input('process_cid')));			
			$this->assign('htmlpath',$this->mbispc);
			return $this->fetch('catecreate');
		}	
	}	

	//处理列表生成
	public function docreate($entitle,$retpl,$type,$i=2){
		$htmlpath = $this->mbispc==0?config('webhtmlpath'):config('webmhtmlpath');
		if($type==1||$type==2){
			$dir = ROOT_PATH.$htmlpath.'/'.$entitle;			
			if (!file_exists($dir)){				
				mkdir ($dir,0777,true);
			}			
			$tplpath = $dir.'/index.html'; 			
			$this->filePutContents($tplpath,$retpl);
			return $htmlpath.'/'.$entitle.'/index.html';
		}elseif($type==3){
			$dir = ROOT_PATH.$htmlpath.'/'.$entitle;			
			if (!file_exists($dir)){				
				mkdir ($dir,0777,true);
			}			
			$tplpath = $dir.'/page_'.$i.'.html';			
			$this->filePutContents($tplpath,$retpl);
			return $htmlpath.'/'.$entitle.'/page_'.$i.'.html';
		}
	}
	
	//生成详情页
	public function detail(){
		$list = db('nav')->where('mid<>6 and isshow=1')->field('id,title,entitle')->order('sort,id')->select();	
		foreach($list as $key=>$v){
			$list[$key]['count'] = db('article')->where('nid',$v['id'])->count();
		}
		$this->assign("list",$list);
		return $this->fetch();
	}	
	
	
	//生成详情页处理
	public function doDetail(){
		$nid     = input('param.process_nid','','intval');
		$startId = input('param.process_startId','','intval');
		$endId   = input('param.process_endId','','intval');
		$num     = input('param.process_num','','intval');
		$cid     = input('param.process_cid','','intval');
		if($this->request->isPost()){
			$where['n.mid']   = array('neq',6);
			$where['n.isshow'] = 1;
			$page = input('param.process_page','','intval');
			$nid && $where['c.nid']    = $nid;
			$cid && $where['c.cid']    = array('in',treecatewhere($cid));
			$startId && $where['c.id'] = array('egt',$startId);
			$endId && $where['c.id']   = array('elt',$endId);	
			$startId && $endId && $where['c.id'] = array('between',[$startId,$endId]);
			$start =($page-1) * $num;
			$list  = db('article')->alias('c')->join("nav n","c.nid=n.id")->where($where)->field("c.id,c.nid,n.entitle")->limit($start,$num)->order("c.istop desc,c.sort desc,c.id desc")->select();
			$count = db('article')->alias('c')->join("nav n","c.nid=n.id")->where($where)->limit($start,$num)->count();			
			if($list){
				$tplpath = array();
				foreach($list as $key=>$v){
					$result = action('index/show/doindex',array('id'=>$v['id'],'mbispc'=>$this->mbispc)); //调用index控制器方法传entitle
					$retpl  = 'show'.$result; //生成目录名称
					$htmlpath = $this->mbispc==0?config('webthemes'):config('webmthemes');					
					$tpl = ROOT_PATH.'app/index/view/'.$htmlpath.'/'.$retpl.'.html'; //模板路径
					if(file_exists($tpl)){
						$redata = $this->doDetailCreate($v['id'],$v['entitle'],$retpl);
						array_push($tplpath,$redata);
						$per = ceil($count/$num);
						$process = ceil($page/$per*100);
					}else{
						array_push($tplpath,$retpl.'模板不存在');
					}
				}
				return json(array('status'=>'1','info'=>'生成中','data'=>$tplpath,'process'=>$process));
			}else{
				return json(array('status'=>'0','info'=>'加载完成','data'=>'生成完成'));
			}			
		}else{
			$this->assign('nid',$nid);
			$this->assign('cid',$cid);
			$this->assign('startId',$startId);
			$this->assign('endId',$endId);
			$this->assign('num',$num);
			$this->assign('isdetail',true);
			$this->assign('htmlpath',$this->mbispc);
			return $this->fetch("create");
		}
	}	

	
	//处理详情页生成
	public function doDetailCreate($id,$entitle,$retpl){
		$htmlpath = $this->mbispc==0?config('webhtmlpath'):config('webmhtmlpath');
		$dir = ROOT_PATH.$htmlpath.'/'.$entitle;	
		if (!file_exists($dir)){				
			mkdir ($dir,0777,true);
		}			
		$tplpath = $dir.'/'.$id.'.html'; 
		$this->filePutContents($tplpath,$retpl);
		return $htmlpath.'/'.$entitle.'/'.$id.'.html';	
	}	
	
	//写入
	private function filePutContents($filepath,$tpl){
		ob_start();
		$webthemes = $this->mbispc==0?config('webthemes'):config('webmthemes'); 
		$content = $this->fetch('index@'.$webthemes.'/'.$tpl);
		echo $content;
		$_cache=ob_get_contents();
		ob_end_clean();
		if($_cache){
			$File = new \think\template\driver\File();
			$File->write($filepath, $_cache);
		}
		ob_flush();
	}

}